<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cookie</title>
<script type="text/javascript">
	function setCookie(name, value, expireDays)	{
		
		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate() + expireDays);
		document.cookie = name + "=" + escape(value) 
			+ ((expireDays == null) ? "" : ";expires=" + expireDate.toUTCString());
	}
	
	function getCookie(name) {
		
		if (document.cookie.length > 0)	{
			startIndex = document.cookie.indexOf(name + "=");
			if (startIndex != -1) {
				startIndex = startIndex + name.length + 1;
				endIndex = document.cookie.indexOf(";", startIndex);
				if (endIndex == -1) {
					endIndex = document.cookie.length;
				}
				return unescape(document.cookie.substring(startIndex, endIndex));
			}
		}
		return "";
	}
	
	function checkCookie() {
		
		userName = getCookie("user_name");
		if (userName !=null && userName != "") {
			alert("Welcome again " + userName + "!");
		} else {
			userName = prompt("Please enter your name:", "");
			if (userName != null && userName != "") {
				setCookie("user_name", userName, 1);
			}
		} 
	}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>